package com.bishe.demo.modules.hotel.hotelRoom.service.impl;

import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.hotel.common.dao.HotelImageDao;
import com.bishe.demo.modules.hotel.common.entity.HotelImage;
import com.bishe.demo.modules.hotel.hotelRoom.dao.*;
import com.bishe.demo.modules.hotel.hotelRoom.entity.*;
import com.bishe.demo.modules.hotel.hotelRoom.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomDao roomDao;

    @Autowired
    private BedDao bedDao;

    @Autowired
    private RoomBedDao roomBedDao;

    @Autowired
    private RoomUserDao roomUserDao;

    @Autowired
    private RoomHotelImageDao roomHotelImageDao;

    @Autowired
    private HotelImageDao hotelImageDao;

    @Override
    @Transactional
    public ResultEntity<Room> insertRoom(Room room) {
        roomDao.insertRoom(room);
        roomUserDao.insertRoomUser(new RoomUser(room.getRoomId(), room.getUser().getUserId()));

        List<Bed> beds = room.getBeds();
        for (Bed bed : beds) {
            bedDao.insertBed(bed);
            roomBedDao.insertRoomBed(new RoomBed(room.getRoomId(), bed.getBedId()));
        }

        List<HotelImage> hotelImages = room.getHotelImages();

        for (HotelImage hotelImage : hotelImages) {
            hotelImageDao.insertImage(hotelImage);
            roomHotelImageDao.insertRoomHotelImage(new RoomHotelImage(room.getRoomId(), hotelImage.getId()));
        }

        return new ResultEntity<Room>(ResultEntity.ResultStatus.SUCCESS.code, "添加成功", room);
    }

    @Override
    public List<Room> getRoomsByHotelId(int hotelId) {
        return roomDao.getRoomsByHotelId(hotelId);
    }

    @Override
    public ResultEntity<List<Room>> updateRoom(List<Room> rooms) {
        delete(rooms);
        for (Room room : rooms) {
            insertRoom(room);
        }
        return new ResultEntity<List<Room>>(ResultEntity.ResultStatus.SUCCESS.code, "修改成功", rooms);
    }

    public void delete(List<Room> rooms) {
        List<Room> roomsByHotelId = roomDao.getRoomsByHotelId(rooms.get(0).getUser().getUserId());
        for (Room room : roomsByHotelId) {
            List<Bed> bedsByRoomId = bedDao.getBedsByRoomId(room.getRoomId());
            for (Bed bed : bedsByRoomId) {
                bedDao.deleteBedsByBedId(bed.getBedId());
            }
            roomBedDao.deleteRoomBedByRoomId(room.getRoomId());
            roomUserDao.deleteRoomUserByUserId(room.getUser().getUserId());
            List<HotelImage> imagesByRoomId = hotelImageDao.getImagesByRoomId(room.getRoomId());
            for (HotelImage hotelImage : imagesByRoomId) {
                hotelImageDao.deleteImageByImageId(hotelImage.getId());
            }
            roomHotelImageDao.deleteRoomImageByRoomId(room.getRoomId());
            roomDao.deleteRoomByRoomId(room.getRoomId());
        }
    }
}
